package cn.kazuz.service.login.controller;

import cn.kazuz.common.util.annotation.Authorization;
import cn.kazuz.common.util.result.Result;
import cn.kazuz.service.login.entity.AdminLogin;
import cn.kazuz.service.login.entity.vo.AdminLoginVo;
import cn.kazuz.service.login.service.AdminInfoService;
import cn.kazuz.service.login.utils.JwtUtils;
import lombok.NonNull;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("admin_info")
@CrossOrigin
public class AdminInfoController {

	@Autowired
	private AdminInfoService adminInfoService;

	/**
	 * 根据token取当前用户信息
	 *
	 * @param token
	 * @return
	 */
	@Authorization
	@GetMapping("getInfo")
	public Result getInfo(@NonNull @Param("token") String token) {
		String adminId = JwtUtils.getIdByToken(token);
		AdminLogin adminLogin = adminInfoService.getById(adminId);
		AdminLoginVo adminLoginVo = new AdminLoginVo();
		BeanUtils.copyProperties(adminLogin, adminLoginVo);
		return Result.ok().data("adminId", adminLoginVo.getAdminId())
				.data("adminUsername", adminLoginVo.getAdminUsername())
				.data("adminPassword", adminLoginVo.getAdminPassword())
				.data("adminPhone", adminLoginVo.getAdminPhone())
				.data("createTime", adminLoginVo.getCreateTime())
				.data("modifyTime", adminLoginVo.getModifyTime())
				.data("adminAvatar", adminLoginVo.getAdminAvatar());
	}

}
